package com.example.compose

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintSet
import androidx.constraintlayout.compose.ExperimentalMotionApi
import androidx.constraintlayout.compose.MotionLayout
import androidx.constraintlayout.compose.MotionScene
import com.example.compose.ui.theme.ComposeTheme

@Preview
@Composable
fun DynamicIsLand4Preview() {
        DynamicIsLand4()
}

@OptIn(ExperimentalMotionApi::class)
@Composable
fun DynamicIsLand4() {
    var expanded by remember {
        mutableStateOf(value = false)
    }
    val progress by animateFloatAsState(
        targetValue = if (expanded) 1f else 0f,
        animationSpec = tween(durationMillis = 500),
        label = "progress"
    )
    var colorBackgroundState by remember {
        mutableStateOf(Color(0xFF039BE5))
    }
    var heightState by remember {
        mutableStateOf(48.dp)
    }
    var widthState by remember {
        mutableStateOf(196.dp)
    }
    var percentCornerState by remember {
        mutableIntStateOf(value = 50)
    }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        contentAlignment = Alignment.Center
    ) {
        MotionLayout(
            modifier = Modifier
                .width(widthState)
                .height(heightState)
                .clip(shape = RoundedCornerShape(percent = percentCornerState))
                .background(colorBackgroundState)
                .clickable { expanded = !expanded },
            motionScene = getMotionSceneForDemo4(),
            progress = progress
        ) {
            val colorBackground = motionProperties(id = "surface").value.color("color")
            colorBackground.let { colorBackgroundState = it }

            val newHeight = motionProperties(id = "surface").value.float("height")
            newHeight.let { heightState = it.dp }

            val newWidth = motionProperties(id = "surface").value.float("width")
            newWidth.let { widthState = it.dp }

            val percentCorner = motionProperties(id = "surface").value.int("corner")
            percentCorner.let { percentCornerState = it }

//            Text(text = "logo", modifier = Modifier.layoutId("solanaLogo"))

            Text(
                text = "+1.96%",
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                color = Color(0xffffffff),
                modifier = Modifier.layoutId("labelText")
            )
            Image(
                painter = painterResource(id = R.drawable.heart),
                contentDescription = "solanaChart",
                contentScale = ContentScale.Crop,
                modifier = Modifier.layoutId("solanaChart")
            )

            //
            Image(
                painter = painterResource(id = R.drawable.nox),
                contentDescription = "binanceLogo",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .layoutId("binanceLogo")
                    .size(24.dp)
            )
            RowLabel(modifier = Modifier.layoutId("rowLabel"))
            RowPrice(modifier = Modifier.layoutId("rowPrice"))
            RowButtons(modifier = Modifier.layoutId("rowButtons"))
        }
    }
}

@Composable
fun RowLabel(modifier: Modifier) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(start = 16.dp, end = 16.dp),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(text = "深圳", fontSize = 18.sp, color = Color(0xffffffff))
        Text(text = "广州", fontSize = 18.sp, color = Color(0xffffffff))
        Text(
            text = "北京",
            fontSize = 18.sp,
            fontWeight = FontWeight.Bold,
            style = TextStyle(textDecoration = TextDecoration.Underline),
            color = Color(0xFFFBC02D)
        )
        Text(text = "上海", fontSize = 18.sp, color = Color(0xffffffff))
    }
}

@Composable
fun RowPrice(modifier: Modifier) {
    Column(
        modifier = modifier
            .fillMaxWidth()
            .padding(start = 16.dp, end = 16.dp)
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Image(
                painter = painterResource(id = R.drawable.koda),
                contentDescription = "solanaLogo",
                contentScale = ContentScale.Crop,
                modifier = Modifier.size(24.dp)
            )
            Text(
                text = "北京",
                color = Color.Green,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(start = 8.dp)
            )
        }
        Row(
            modifier = modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = "234234,345",
                color = Color.White,
                fontSize = 30.sp,
                fontWeight = FontWeight.Bold
            )
            Text(
                text = "+1.69%",
                color = Color.White,
                modifier = Modifier
                    .clip(shape = RoundedCornerShape(percent = 50))
                    .background(color = Color(color = 0xff29c57a))
                    .padding(8.dp)
            )
        }
    }
}

@Composable
fun RowButtons(modifier: Modifier) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(start = 16.dp, end = 16.dp),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(
            text = "买",
            textAlign = TextAlign.Center,
            fontWeight = FontWeight.Bold,
            color = Color.White,
            modifier = Modifier
                .padding(end = 4.dp)
                .weight(1f)
                .clip(shape = RoundedCornerShape(percent = 50))
                .background(color = Color(0xff29c57a))
                .padding(16.dp)
        )
        Text(
            text = "卖",
            textAlign = TextAlign.Center,
            fontWeight = FontWeight.Bold,
            color = Color.White,
            modifier = Modifier
                .padding(start = 4.dp)
                .weight(1f)
                .clip(shape = RoundedCornerShape(percent = 50))
                .background(color = Color(0xFFC94475))
                .padding(16.dp)
        )
    }
}

@OptIn(ExperimentalMotionApi::class)
@Composable
fun getMotionSceneForDemo4(): MotionScene {
    return MotionScene {
        val start1 = constraintSet {
            val surface = createRefFor("surface")
            val solanaLogo = createRefFor("solanaLogo")
            val labelText = createRefFor("labelText")
            val solanaChart = createRefFor("solanaChart")
            val binanceLogo = createRefFor("binanceLogo")
            val rowLabel = createRefFor("rowLabel")
            val rowPrice = createRefFor("rowPrice")
            val rowButton = createRefFor("rowButtons")

            constrain(surface) {
                customFloat("width", 196f)
                customFloat("height", 48f)
                customFloat("corner", 50f)
                customColor("color", Color(0xFF039BE5))
            }
            constrain(solanaLogo) {
                top.linkTo(parent.top)
                start.linkTo(parent.start, margin = 8.dp)
                bottom.linkTo(parent.bottom)
            }

            constrain(labelText) {
                top.linkTo(parent.top)
                start.linkTo(solanaLogo.end, margin = 8.dp)
                bottom.linkTo(parent.bottom)
            }

            constrain(solanaChart) {
                top.linkTo(parent.top)
                end.linkTo(parent.end, 16.dp)
                bottom.linkTo(parent.bottom)
            }

            //
            constrain(binanceLogo) {
                top.linkTo(parent.top, 22.dp)
                start.linkTo(parent.start, 22.dp)
                scaleX = 0f
                scaleY = 0f
                alpha = 0f
            }
            constrain(rowLabel) {
                top.linkTo(binanceLogo.bottom, 16.dp)
                start.linkTo(parent.start, 22.dp)
                end.linkTo(parent.end, 22.dp)
                scaleX = 0f
                scaleY = 0f
                alpha = 0f
            }
            constrain(rowPrice) {
                top.linkTo(rowLabel.bottom, 16.dp)
                start.linkTo(parent.start, 22.dp)
                end.linkTo(parent.end, 22.dp)
                scaleX = 0f
                scaleY = 0f
                alpha = 0f
            }
            constrain(rowButton) {
                top.linkTo(rowPrice.bottom, 8.dp)
                start.linkTo(parent.start, 22.dp)
                end.linkTo(parent.end, 22.dp)
                scaleX = 0f
                scaleY = 0f
                alpha = 0f
            }
        }

        val end1 = constraintSet {
            val surface = createRefFor("surface")
            val solanaLogo = createRefFor("solanaLogo")
            val labelText = createRefFor("labelText")
            val solanaChart = createRefFor("solanaChart")
            val binanceLogo = createRefFor("binanceLogo")
            val rowLabel = createRefFor("rowLabel")
            val rowPrice = createRefFor("rowPrice")
            val rowButton = createRefFor("rowButtons")

            constrain(surface) {
                customFloat("width", 340f)
                customFloat("height", 239f)
                customFloat("corner", 10f)
                customColor("color", Color(0xFF1E88E5))
            }
            constrain(solanaLogo) {
                top.linkTo(parent.top)
                start.linkTo(parent.start)
                bottom.linkTo(parent.bottom)
                scaleX = 0f
                scaleY = 0f
                alpha = 0f
            }

            constrain(labelText) {
                top.linkTo(parent.top)
                start.linkTo(solanaLogo.end)
                bottom.linkTo(parent.bottom)
                scaleX = 0f
                scaleY = 0f
                alpha = 0f
            }

            constrain(solanaChart) {
                top.linkTo(parent.top)
                end.linkTo(parent.end)
                bottom.linkTo(parent.bottom)
                scaleX = 0f
                scaleY = 0f
                alpha = 0f
            }

            //
            constrain(binanceLogo) {
                top.linkTo(parent.top, 22.dp)
                start.linkTo(parent.start, 22.dp)
                scaleX = 1f
                scaleY = 1f
            }
            constrain(rowLabel) {
                top.linkTo(binanceLogo.bottom, 16.dp)
                start.linkTo(parent.start, 22.dp)
                end.linkTo(parent.end, 22.dp)
                scaleX = 1f
                scaleY = 1f
            }
            constrain(rowPrice) {
                top.linkTo(rowLabel.bottom, 16.dp)
                start.linkTo(parent.start, 22.dp)
                end.linkTo(parent.end, 22.dp)
                scaleX = 1f
                scaleY = 1f
            }
            constrain(rowButton) {
                top.linkTo(rowPrice.bottom, 8.dp)
                start.linkTo(parent.start, 22.dp)
                end.linkTo(parent.end, 22.dp)
                scaleX = 1f
                scaleY = 1f
            }
        }
        transition(from =  start1, to = end1, name = "default") {}
    }
}